package com.example.lenovo.weiyi;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.com.ruanmeng.demon.ChangePswM;
import com.com.ruanmeng.utils.CommonUtil;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

/**
 * Created by Administrator on 2017/5/5.
 */

public class ChangePswActivity extends BaseActivity {
    @BindView(R.id.et_old_psw)
    EditText etOldPsw;
    @BindView(R.id.et_new_psw)
    EditText etNewPsw;
    @BindView(R.id.btn_ok)
    Button btnOk;
    @BindView(R.id.cb_cpsw_old)
    CheckBox cbCpswOld;
    @BindView(R.id.cb_cpsw_new)
    CheckBox cbCpswNew;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_psw);
        ButterKnife.bind(this);
        AddActivity(this);
        ChangLayTitle("修改密码");
        init();
    }

    public void init() {
        etOldPsw.addTextChangedListener(this);
        etNewPsw.addTextChangedListener(this);
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        super.onTextChanged(charSequence, i, i1, i2);
        if (CommonUtil.getEditText(etOldPsw).isEmpty()) {
            cbCpswOld.setVisibility(View.GONE);
            cbCpswOld.setChecked(false);
        } else {
            cbCpswOld.setVisibility(View.VISIBLE);
        }
        if (CommonUtil.getEditText(etNewPsw).isEmpty()) {
            cbCpswNew.setVisibility(View.GONE);
            cbCpswNew.setChecked(false);
        } else {
            cbCpswNew.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.btn_ok, R.id.cb_cpsw_old, R.id.cb_cpsw_new})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_ok:
                if (CommonUtil.getEditText(etOldPsw).isEmpty()) {
                    CommonUtil.showToask(this, "请输入原密码！");
                    return;
                }
                if (!PreferencesUtils.getString(ChangePswActivity.this, "psw").equals(etOldPsw.getText().toString())) {
                    CommonUtil.showToask(this, "请输入正确的原密码！");
                    return;
                }
                if (CommonUtil.getEditText(etNewPsw).isEmpty()) {
                    CommonUtil.showToask(this, "请输入新密码！");
                    return;
                }
                if (CommonUtil.getEditText(etNewPsw).length() < 6) {
                    CommonUtil.showToask(this, "请输入6-12位密码！");
                    return;
                }
                Update();
                break;
            case R.id.cb_cpsw_old:
                if (cbCpswOld.isChecked()) {
                    etOldPsw.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    etOldPsw.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                break;
            case R.id.cb_cpsw_new:
                if (cbCpswNew.isChecked()) {
                    etNewPsw.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    etNewPsw.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                break;
        }

    }

    public void Update() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.ChangePsw, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(ChangePswActivity.this, "uid"));
        mRequest.add("oldPwd", etOldPsw.getText().toString());
        mRequest.add("newPwd", etNewPsw.getText().toString());
        mRequest.add("newPwd2", etNewPsw.getText().toString());
        // 添加到请求队列
        CallServer.getRequestInstance().add(ChangePswActivity.this, 0, mRequest,
                new CustomHttpListener(ChangePswActivity.this, true, ChangePswM.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        ChangePswM model = ((ChangePswM) data);
                        PreferencesUtils.putInt(ChangePswActivity.this, "Login", 0);//登陆标记
                        Intent in = new Intent(ChangePswActivity.this, LoginActivity.class);
                        startActivity(in);
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        CommonUtil.showToask(ChangePswActivity.this, obj.getString("msg"));
                    }
                }, true, true);
    }

}
